package cn.itguy.mobileguard.widget;

import java.util.List;

import cn.itguy.mobileguard.R;

import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * Adapter for the {@link ViewPager}.  Unlike {@link FragmentPagerAdapter},
 * {@link #instantiateItem} returns existing fragments, and {@link #instantiateItem}/
 * {@link #destroyItem} show/hide fragments instead of attaching/detaching.
 *
 * In search mode, we always show the "all" fragment, and disable the swipe.  We change the
 * number of items to 1 to disable the swipe.
 *
 * TODO figure out a more straight way to disable swipe.
 */
public class TabPagerAdapter extends PagerAdapter {
	
	private List<Fragment> fragments;
	
    private final FragmentManager mFragmentManager;
    
    private FragmentTransaction mCurTransaction = null;

    private Fragment mCurrentPrimaryItem;

    public TabPagerAdapter(FragmentManager fm, List<Fragment> fs) {
        mFragmentManager = fm;
        
        fragments = fs;
        
        mCurTransaction = mFragmentManager.beginTransaction();
        for (int i = 0; i < fragments.size(); i++) {
        	Fragment fragment = fragments.get(i);
        	mCurTransaction.add(R.id.contentPager, fragment);
        	mCurTransaction.hide(fragment);
        }
        
        mCurTransaction.commitAllowingStateLoss();
        mCurTransaction = null;
        mFragmentManager.executePendingTransactions();
        
    }

    @Override
    public int getCount() {
    	return fragments.size();
    }

    @Override
    public void startUpdate(View container) {
    	
    }
    
	public Fragment getItem(int position) {
		return fragments.get(position);
	}

    @Override
    public Object instantiateItem(View container, int position) {
        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }
        Fragment f = getItem(position);
        mCurTransaction.show(f);

        // Non primary pages are not visible.
        f.setUserVisibleHint(f == mCurrentPrimaryItem);
        f.setMenuVisibility(f == mCurrentPrimaryItem);
        
        return f;
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        if (mCurTransaction == null) {
            mCurTransaction = mFragmentManager.beginTransaction();
        }
        mCurTransaction.hide((Fragment) object);
    }

    @Override
    public void finishUpdate(View container) {
        if (mCurTransaction != null) {
            mCurTransaction.commitAllowingStateLoss();
            mCurTransaction = null;
            mFragmentManager.executePendingTransactions();
        }
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return ((Fragment) object).getView() == view;
    }

    @Override
    public void setPrimaryItem(View container, int position, Object object) {
        Fragment fragment = (Fragment) object;
        if (mCurrentPrimaryItem != fragment) {
            if (mCurrentPrimaryItem != null) {
                mCurrentPrimaryItem.setUserVisibleHint(false);
                mCurrentPrimaryItem.setMenuVisibility(false);
            }
            if (fragment != null) {
                fragment.setUserVisibleHint(true);
                fragment.setMenuVisibility(true);
            }
            mCurrentPrimaryItem = fragment;
        }
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }
}
